package demo.concurrent.thread;

/**
 * yield方法测试
 */
public class YieldDemo {

    private volatile boolean exist = false;

    public static void main(String[] args) throws InterruptedException {
        new YieldDemo().test();
    }

    public void test() throws InterruptedException {
        Object lock = new Object();
        Thread threadA = new Thread(() -> {
            while (!exist) {
                System.out.println("正在执行");
            }
            System.out.println("执行完毕");
        }, "yield_demo_thread");

        Thread threadB = new Thread(() -> {
            int i = 0;
            while (i < 100) {
                i++;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            exist = true;
        });
        threadA.start();
        threadB.start();
        Thread.sleep(10000);
    }

}
